﻿Imports ZViewTVDotNet.SQLiteWrapper

Public Module Playlist_Channel
    Private PenultiemeDtSinglechannel As New DataTable
    Private dtSingleChannel As New DataTable
    Private listChannels As Channels()
    Private memoOrdering As Integer
    Private CompteChaines As Integer = 0
    Public Sub ChooseChannelPlus()
        If listChannels IsNot Nothing Then
            If ChooseChanneById(CInt(listChannels(Chainedemande("plus")).Id)) Then
                Watch()
            End If
        End If
    End Sub

    Public Sub ChooseChannelMoins()
        If listChannels IsNot Nothing Then
            If ChooseChanneById(CInt(listChannels(Chainedemande("moins")).Id)) Then
                Watch()
            End If
        End If
    End Sub
    Public Function ChannelNameByOrdering(ByVal ordering As Integer) As String
        If ordering < listChannels.Length AndAlso ordering > 0 Then
            Return listChannels(ordering).Name.ToString
        Else
            Return "Pas de chaîne"
        End If

    End Function
    Public Function ChannelMoins() As String()
        If listChannels IsNot Nothing Then

            If memoOrdering > 1 Then
                memoOrdering -= 1
            Else
                memoOrdering = listChannels.Length
            End If
            Return {listChannels(memoOrdering - 1).Name, listChannels(memoOrdering - 1).ordering.ToString}
        Else
            Return {"", ""}
        End If

    End Function

    Public Function ChannelPlus() As String()
        If listChannels IsNot Nothing Then

            If memoOrdering < listChannels.Length - 1 Then
                memoOrdering += 1
            Else
                memoOrdering = 1
            End If
            Return {listChannels(memoOrdering - 1).Name, listChannels(memoOrdering - 1).ordering.ToString}
        Else
            Return {"", ""}
        End If
    End Function

    Private Function Chainedemande(ByVal PlusMoins As String) As Integer
        Dim Channel As Integer = CInt(dtSingleChannel.Rows(0).Item(5))
        Dim retour As Integer = 0

        Select Case PlusMoins
            Case "plus"
                If Channel < listChannels.Length Then
                    retour = Channel
                Else
                    retour = 0
                End If
            Case "moins"
                If Channel > 1 Then
                    retour = Channel - 2
                Else
                    retour = listChannels.Length - 1
                End If
            Case Else
                retour = 0
        End Select

        Return retour
    End Function

    Public Function ChooseChannelByOrdering(ByVal ordering As Integer) As Boolean
        If ordering <= listChannels.Length AndAlso ordering > 0 Then
            Return ChooseChanneById(CInt(listChannels(ordering - 1).Id))
        Else
            Return False
        End If
    End Function

    Public Function ToggleChannel() As Boolean
        If dtSingleChannel IsNot Nothing AndAlso dtSingleChannel.Rows.Count > 0 AndAlso PenultiemeDtSinglechannel IsNot Nothing AndAlso PenultiemeDtSinglechannel.Rows.Count > 0 Then
            Dim memopenulmtiemedtsinglechannel As DataTable = PenultiemeDtSinglechannel
            PenultiemeDtSinglechannel = dtSingleChannel
            dtSingleChannel = memopenulmtiemedtsinglechannel
            Return Not CInt(dtSingleChannel.Rows(0).Item(0)) = CInt(PenultiemeDtSinglechannel.Rows(0).Item(0))
        Else
            Return False
        End If
    End Function

    Public Function ChooseChanneById(ByVal IdChannel As Integer) As Boolean
        Dim sql As String
        Dim db As SQLiteBase = New SQLiteBase
        Dim memoDtSingleChannel As Integer
        If dtSingleChannel Is Nothing OrElse dtSingleChannel.Rows.Count = 0 Then
            memoDtSingleChannel = 0
        Else
            memoDtSingleChannel = CInt(dtSingleChannel.Rows(0).Item(0))
            PenultiemeDtSinglechannel = dtSingleChannel
        End If
        db.OpenDatabase(BDDPath & "Channels.db3")
        sql = "select * from channel where id =" & IdChannel.ToString

        dtSingleChannel = db.ExecuteQuery(sql)
        db.CloseDatabase()
        memoOrdering = CInt(dtSingleChannel.Rows(0).Item("ordering"))
        Return Not CInt(dtSingleChannel.Rows(0).Item(0)) = memoDtSingleChannel
    End Function

    Public Function ChooseChannelByZgId(ByVal zgid As String) As Boolean
        Dim sql As String
        Dim db As SQLiteBase = New SQLiteBase
        Dim memoDtSingleChannel As Integer
        If dtSingleChannel Is Nothing Then
            memoDtSingleChannel = 0
        Else
            memoDtSingleChannel = CInt(dtSingleChannel.Rows(0).Item(0))
        End If
        db.OpenDatabase(BDDPath & "Channels.db3")
        sql = "select * from channel where ZGTVDotNETID =" & zgid
        dtSingleChannel = db.ExecuteQuery(sql)
        db.CloseDatabase()
        Return Not CInt(dtSingleChannel.Rows(0).Item(0)) = memoDtSingleChannel
        memoOrdering = CInt(dtSingleChannel.Rows(0).Item("ordering"))
    End Function

    Public Function LoadPlaylist() As ListPlayList()
        Dim playlistList() As ListPlayList

        Dim sql As String
        Dim db As SQLiteBase = New SQLiteBase
        db.OpenDatabase(BDDPath & "Channels.db3")
        sql = "select * from playlist "
        Dim dtPlaylist As DataTable = db.ExecuteQuery(sql)
        Dim i As Integer
        db.CloseDatabase()
        Play()
        ReDim playlistList(dtPlaylist.Rows.Count - 1)
        For i = 0 To dtPlaylist.Rows.Count - 1
            playlistList(i).Id = CLng(dtPlaylist.Rows(i).Item(0))
            playlistList(i).Name = dtPlaylist.Rows(i).Item(1).ToString
        Next
        Return playlistList
    End Function

    Public Function LoadChannelsFromPlaylist(ByVal id As Int64) As Channels()

        Dim sql As String
        Dim db As SQLiteBase = New SQLiteBase
        db.OpenDatabase(BDDPath & "Channels.db3")
        sql = "select title,id,ordering  from Channel where extPlaylistID=" & id.ToString & " order by ordering"
        Dim dtChannels As DataTable = db.ExecuteQuery(sql)
        Dim i As Integer
        db.CloseDatabase()
        ReDim listChannels(dtChannels.Rows.Count - 1)
        CompteChaines = dtChannels.Rows.Count
        For i = 0 To dtChannels.Rows.Count - 1
            listChannels(i).Name = dtChannels.Rows(i).Item(0).ToString
            listChannels(i).Id = CLng(dtChannels.Rows(i).Item(1))
            listChannels(i).ordering = CInt(dtChannels.Rows(i).Item(2))
        Next
        Return listChannels
    End Function

#Region "Structure"

    Public Structure ListPlayList
        Public Name As String
        Public Id As Int64
    End Structure

    Public Structure Channels
        Public Name As String
        Public Id As Int64
        Public ordering As Integer
    End Structure

#End Region

#Region "property"

    Public ReadOnly Property title As String
        Get
            Return dtSingleChannel.Rows(0).Item("title").ToString
        End Get
    End Property

    Public ReadOnly Property Extension As String()
        Get
            Return dtSingleChannel.Rows(0).Item("Extension").ToString.Split(CChar(":"))
        End Get
    End Property

    Public ReadOnly Property Location As String
        Get
            Return dtSingleChannel.Rows(0).Item("Location").ToString
        End Get
    End Property

    Public ReadOnly Property ID As Integer
        Get
            If dtSingleChannel IsNot Nothing AndAlso dtSingleChannel.Rows.Count > 0 Then
                Return CInt(dtSingleChannel.Rows(0).Item("Id"))
            Else
                Return 0
            End If
        End Get
    End Property
    Public ReadOnly Property Ordering As Integer
        Get
            Return CInt(dtSingleChannel.Rows(0).Item("ordering"))
        End Get
    End Property
    Public ReadOnly Property ChannelCount As Integer
        Get
            Return CompteChaines
        End Get
    End Property
#End Region

End Module
